/*
Script: String.js

Copyright 2005-2007 Netvibes Ltd.
All rights reserved.

Credits:
    Partially based on Prototype Framework - (c) 2005-2007 Sam Stephenson
*/

UWA.merge(String.prototype, {
  
  trim: function(){
		return this.replace(/^\s+/, '').replace(/\s+$/, '');
	},
	
	isEmail: function() {
      var regexp = /^([a-zA-Z0-9_.\-+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      return regexp.test(this);
  },
	
  stripTags: function() {
		return this.replace(/<\/?[^>]+>/gi, '');
	},
	
	truncate: function(length, truncation) {
      length = length || 30;
      truncation = truncation === undefined ? '...' : truncation;
      return this.length > length ?
          this.slice(0, length - truncation.length) + truncation : this;
  },
  
  s: function() {
    var str = this;
    if (arguments.length < 1) return str;
    var re = /([^%]*)%s(.*)/;
    var a = [], numSubstitutions = -1;
    while (a = re.exec(str)) {
        var leftpart = a[1], rightPart = a[2];
        if (++numSubstitutions >= arguments.length) {
            break;
        }
        str = leftpart + arguments[numSubstitutions] + rightPart;
    }
    return str;
  },
  
  escapeRE: function() {
    return this.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
  }
	
});